package com.example.back.controller;

import com.example.back.entity.Notice;
import com.example.back.entity.otd.NoticeCommentRelation;
import com.example.back.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

@RestController
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PostMapping("/notice")
    public int addNotice(Notice notice){
        return noticeService.addNotice(notice);
    }

    @DeleteMapping("/notice/{id}")
    public int deleteNotice(@PathVariable("id") Serializable id){
        return noticeService.deleteNotice(id);
    }

    @PutMapping("/notice")
    public int updateNotice(Notice notice){
        return noticeService.updateNotice(notice);
    }

    @GetMapping("/notice/{id}")
    public Notice selectNoticeById(@PathVariable("id") Serializable id){
        return noticeService.selectNoticeById(id);
    }

    @GetMapping("/notice")
    public ArrayList<Notice> selectNotice(@RequestParam Integer userId,
                                          @RequestParam(required = false) String type)
    {
        // 根据userId和type查询通知列表
        Notice noticeQuery = new Notice();
        noticeQuery.setUserId(userId);
        noticeQuery.setType(type);

        return noticeService.selectNotice(noticeQuery);
    }

    @GetMapping("/notice/comment/{noticeId}")
    public NoticeCommentRelation selectNoticeCommentRelationById(@PathVariable("noticeId") Serializable noticeId) {
        return noticeService.selectNoticeCommentRelationById(noticeId);
    }

    @PutMapping("/notice/{id}/read")
    public int markNoticeAsRead(@PathVariable("id") Serializable id) {
        return noticeService.markNoticeAsRead(id);
    }

    /**
     * 获取用户的未读通知计数
     */
    @GetMapping("/notice/unread-count")
    public Map<String, Integer> getUnreadNoticeCount(@RequestParam Integer userId) {
        return noticeService.getUnreadNoticeCount(userId);
    }

    /**
     * 获取用户各类通知的未读计数
     */
    @GetMapping("/notice/unread-counts")
    public Map<String, Integer> getUnreadNoticeCounts(@RequestParam Integer userId) {
        return noticeService.getUnreadNoticeCounts(userId);
    }
}
